package qq.listener;

import catcode.CatCodeUtil;
import catcode.CodeBuilder;
import com.alibaba.fastjson.JSONObject;
import love.forte.common.ioc.annotation.Beans;
import love.forte.simbot.annotation.Filter;
import love.forte.simbot.annotation.OnGroup;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.sender.MsgSender;
import love.forte.simbot.filter.MatchType;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import qq.tool.SeTuTool;
import qq.utils.ThreadUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Beans
public class SeTuListener {
    private static CloseableHttpClient httpclient = HttpClients.createDefault();
    final CatCodeUtil catUtil = CatCodeUtil.INSTANCE;
    //色图
    @OnGroup
    @Filter(value = "给我康康", matchType = MatchType.STARTS_WITH)
    public void privateMsg(GroupMsg groupMsg, MsgSender sender) throws IOException {
        String message = groupMsg.getMsg();
        if (message.contains("给我康康")){
            String[] s1 = message.split(" ");
            String keyword = "";
            String r18="0";
            String num="1";
            if(s1.length>1){
                keyword = s1[1];
            }
            if(s1.length>2){
                if(Integer.parseInt(s1[2])>0 && Integer.parseInt(s1[2])<=20)
                num = s1[2];
            }
            if(s1.length>3){
                r18="1";
            }
            RequestBuilder requestBuilder = RequestBuilder.get("https://api.lolicon.app/setu/v2?keyword="+keyword+"&r18="+r18+"&num="+num);
            CloseableHttpResponse response = httpclient.execute(requestBuilder.build());
            HttpEntity httpEntity = response.getEntity();
            String s = EntityUtils.toString(httpEntity, "UTF-8");
            JsonRootBean jsonRootBean = JSONObject.parseObject(s, JsonRootBean.class);
//            System.out.println(jsonRootBean.getData().get(0).getUrls().getOriginal());
            CodeBuilder<String> builder = catUtil.getStringCodeBuilder("image", true);
//            builder.key("")
//            sender.SENDER.sendGroupMsg(groupMsg,"[CAT:image,url="+jsonRootBean.getData().get(0).getUrls().getOriginal()+"]");
            for (Data datum : jsonRootBean.getData()) {
                try{
                    ThreadUtil.scheduledThreadPoolExecutor.execute(new Runnable() {
                        @Override
                        public void run() {
                            sender.SENDER.sendPrivateMsg(groupMsg,"[CAT:image,url="+datum.getUrls().getOriginal()+"]");
                        }
                    });
                }catch (Exception e){
                    System.out.println(e);
                }
            }
            return;
        }
    }
}

class JsonRootBean {

    private String error;
    private List<Data> data;
    public void setError(String error) {
        this.error = error;
    }
    public String getError() {
        return error;
    }

    public void setData(List<Data> data) {
        this.data = data;
    }
    public List<Data> getData() {
        return data;
    }

}
/**
 * Copyright 2022 json.cn
 */

class Urls {

    private String original;
    public void setOriginal(String original) {
        this.original = original;
    }
    public String getOriginal() {
        return original;
    }

}
class Data {

    private long pid;
    private int p;
    private long uid;
    private String title;
    private String author;
    private boolean r18;
    private int width;
    private int height;
    private List<String> tags;
    private String ext;
    private long uploadDate;
    private Urls urls;
    public void setPid(long pid) {
        this.pid = pid;
    }
    public long getPid() {
        return pid;
    }

    public void setP(int p) {
        this.p = p;
    }
    public int getP() {
        return p;
    }

    public void setUid(long uid) {
        this.uid = uid;
    }
    public long getUid() {
        return uid;
    }

    public void setTitle(String title) {
        this.title = title;
    }
    public String getTitle() {
        return title;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
    public String getAuthor() {
        return author;
    }

    public void setR18(boolean r18) {
        this.r18 = r18;
    }
    public boolean getR18() {
        return r18;
    }

    public void setWidth(int width) {
        this.width = width;
    }
    public int getWidth() {
        return width;
    }

    public void setHeight(int height) {
        this.height = height;
    }
    public int getHeight() {
        return height;
    }

    public void setTags(List<String> tags) {
        this.tags = tags;
    }
    public List<String> getTags() {
        return tags;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }
    public String getExt() {
        return ext;
    }

    public void setUploadDate(long uploadDate) {
        this.uploadDate = uploadDate;
    }
    public long getUploadDate() {
        return uploadDate;
    }

    public void setUrls(Urls urls) {
        this.urls = urls;
    }
    public Urls getUrls() {
        return urls;
    }

}
